import math

# ZigZag.py
def zigzag(s: str, row: int):
    n = len(s)

    number_of_sections = math.ceil(n / (2.0 * row - 2.0))
    col_in_a_section = row - 1

    # 保存之字形字符串需要的总列数
    total_colums = number_of_sections * col_in_a_section
    # 总行数 row

    line = [""] * total_colums
    matrix = []

    for i in range(row):
        matrix.append([""] * total_colums)

    curr_str_idx = 0
    curr_row = 0
    curr_col = 0

    while curr_str_idx < n:
        # 填格子 
        while curr_row < row and curr_str_idx < n:
            matrix[curr_row][curr_col] = s[curr_str_idx]
            curr_row += 1
            curr_str_idx += 1

        curr_row -= 2
        curr_col += 1

        while curr_row > 0 and curr_col < total_colums and curr_str_idx < n:
            matrix[curr_row][curr_col] = s[curr_str_idx]
            curr_row -= 1
            curr_col += 1
            curr_str_idx += 1

    # HOMEWORK: 按照之字形的格式打印 matrix 中的内容。
    for line in matrix:
        for character in line:
            print(character, end="")
        print("\n")

    return

zigzag(s="ABCDEFGHIJKLMNOPQRSTUVWXYZ", row=5)
